package it.asvp.asvp_portal.data.dao.impl;

import it.asvp.asvp_portal.data.dao.ConventionDao;
import it.asvp.asvp_portal.data.entity.domain.Convention;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class ConventionDaoImpl implements ConventionDao {

	private static final Log log = LogFactory.getLog(ConventionDaoImpl.class);

	@Autowired
	private SessionFactory sessionFactory;

	public Long persist(Convention transientInstance) {
		log.debug("persisting Convention instance");
		Long id;
		try {
			id=(Long)sessionFactory.getCurrentSession().save(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
		return id;
	}

	public void delete(Convention persistentInstance) {
		log.debug("deleting Convention instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Convention merge(Convention detachedInstance) {
		log.debug("merging Convention instance");
		try {
			Convention result = (Convention) sessionFactory.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Convention findById(java.lang.Long id) {
		log.debug("getting Convention instance with id: " + id);
	
			Criteria cr=sessionFactory.getCurrentSession().createCriteria(Convention.class);
			cr.add(Restrictions.eq("id", id));
			Convention c= (Convention)cr.uniqueResult();
			return c;
		}
	
	@SuppressWarnings("unchecked")
	public List<Convention> findByActive(boolean active) {
		log.debug("getting Convention instance with field active: " + active);
	
			Criteria cr=sessionFactory.getCurrentSession().createCriteria(Convention.class);
			cr.add(Restrictions.eq("active", active));
			List<Convention> list= (List<Convention>)cr.list();
			return list;
		}

	@SuppressWarnings("unchecked")
	public List<Convention> findAll() {
		log.debug("getting all Conventions instances:");
	
			Criteria cr=sessionFactory.getCurrentSession().createCriteria(Convention.class);
			List<Convention> list= (List<Convention>)cr.list();
			return list;
		}
}
